package com.hinnotek.amoeba.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hinnotek.amoeba.R;


/**
 * 自定义透明的dialog
 */
public class DelCustomDialog extends Dialog {
    private String content;
    private TextView message;
    /**
     * 确认和取消按钮
     */
    private Button negtiveBn ,positiveBn;

    public DelCustomDialog(Context context) {
        super(context, R.style.Theme_AppCompat_Dialog);
        initView();

        initEvent();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                if(DelCustomDialog.this.isShowing())
                    DelCustomDialog.this.dismiss();
                break;
        }
        return true;
    }

    private void initView(){
        setContentView(R.layout.dialog_del);
        message = findViewById(R.id.message);
        negtiveBn = (Button) findViewById(R.id.negtive);
        positiveBn = (Button) findViewById(R.id.positive);
        setCanceledOnTouchOutside(true);
//        WindowManager.LayoutParams attributes = getWindow().getAttributes();
//        attributes.alpha=0.5f;
//        getWindow().setAttributes(attributes);
        setCancelable(false);
    }

    public void setContent(String content){
        this.content = content;
        message.setText(content);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        negtiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onNegtiveClick();
                }
            }
        });
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public DelCustomDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }
    public interface OnClickBottomListener{
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();
        /**
         * 点击取消按钮事件
         */
        public void onNegtiveClick();
    }

}